﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Incominginformations;
using MyMES.Read.API.Dtos;

namespace MyMES.Read.API.Application.CommendHandlers.Incominginformations
{
    public class GetIncominginformationsHandlers : IRequestHandler<GetIncominginformationsCommends, APIPaging<List<IncomArrivaSuppDtos>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Arrivalnotice> arrivalnotRepository;
        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IBaseRepository<Incominginformation> incomRepository;
        private readonly IBaseRepository<StoragePurchase> istoRepository;

     

        public GetIncominginformationsHandlers(IMapper mapper, IBaseRepository<Arrivalnotice> arrivalnotRepository, IBaseRepository<Supplier> supplierRepository, IBaseRepository<Incominginformation> incomRepository, IBaseRepository<StoragePurchase> istoRepository)
        {
            this.mapper = mapper;
            this.arrivalnotRepository = arrivalnotRepository;
            this.supplierRepository = supplierRepository;
            this.incomRepository = incomRepository;
            this.istoRepository = istoRepository;
        }

        /// <summary>
        /// 获取入库信息
        /// </summary>
        /// <param name="request">请求接口</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<IncomArrivaSuppDtos>>> Handle(GetIncominginformationsCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<IncomArrivaSuppDtos>> result = new APIPaging<List<IncomArrivaSuppDtos>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                var sql = "SELECT a.*,b.NoticeName,c.SupplierName,d.StoragePurchaseNo FROM incominginformation a LEFT JOIN arrivalnotice b on a.noticeId=b.NoticeId  LEFT JOIN supplier c on a.SupplierId=c.SupplierId   LEFT JOIN storagepurchase d on a.StoragePurchaseId=d.StoragePurchaseId     where IsDlete=false ORDER BY a.StorageId DESC;";

                var incoms = await incomRepository.SelectAsync(sql);
                var arrivas = await arrivalnotRepository.SelectAsync(sql);
                var supps = await supplierRepository.SelectAsync(sql);
                var stos = await istoRepository.SelectAsync(sql);
                var rincom = mapper.Map<List<IncomArrivaSuppDtos>>(incoms);
                var rarriva = mapper.Map<List<IncomArrivaSuppDtos>>(arrivas);
                var rsupp = mapper.Map<List<IncomArrivaSuppDtos>>(supps);
                var rstos = mapper.Map<List<IncomArrivaSuppDtos>>(stos);


                //循环赋值
                foreach (var item in rincom)
                {

                    item.SupplierName = rsupp.FirstOrDefault(x => x.SupplierId == item.SupplierId)?.SupplierName;
                    item.NoticeName = rarriva.FirstOrDefault(x => x.noticeId == item.noticeId)?.NoticeName;
                    /*      item.NoticePurchase = rarriva.FirstOrDefault(x => x.noticeId == item.noticeId).NoticePurchase;*/
                    item.StoragePurchaseNo = rstos.FirstOrDefault(x => x.StoragePurchaseId == item.StoragePurchaseId).StoragePurchaseNo;
                }

                if (!string.IsNullOrEmpty(request.StorageNo))
                {
                    rincom = rincom.Where(x => x.StorageNo.Contains(request.StorageNo)).ToList();
                }

                if (!string.IsNullOrEmpty(request.StorageName))
                {
                    rincom = rincom.Where(x => x.StorageName.Contains(request.StorageName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    rincom = rincom.Where(x => x.SupplierName.Contains(request.SupplierName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.StoragePurchaseNo))
                {
                    rincom = rincom.Where(x => x.StoragePurchaseNo.Contains(request.StoragePurchaseNo)).ToList();
                }
                if (request.StorageDate != null)
                {

                    var filterDate = request.StorageDate.Value.Date.AddDays(1);
                    rincom = rincom.Where(x => x.StorageDate.Date == filterDate).ToList();
                }

                result.TotalCount = rincom.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                rincom = rincom.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                result.data = rincom;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
